#include <stdio.h>
#include<stdlib.h>

#include "stack.h"

int ten_to_two(stack_t *mystack, int *num);
int main(int argc, char *argv[])
{
	// argv[1]每一个字符入栈
	if (argc < 2)
		return 1;
	stack_t *mystack = NULL;
	char i;
	int num = atoi(argv[1]);

	stack_init(&mystack, sizeof(char), 32);
	
	if(ten_to_two(mystack, &num) != 0){
		printf("转换失败！\n");
	}else{
		printf("%d", num);
		putchar('\n');
	}
	stack_destroy(&mystack);

	return 0;
}


int ten_to_two(stack_t *mystack, int *num){	
	int i = 0;
	while (*num != 0) {
		int tem = *num % 2;
			if (-1 == stack_push(mystack, &tem)) {
				printf("栈满了\n");
				return -1;
			}
		*num /= 2;
	}

	while (!stack_empty(mystack)) {
		stack_pop(mystack, &i);
		*num = *num * 10 + i;
	}
	return 0;
}
